package com.springboot.controller;

import com.springboot.dto.DoctorViewPatient;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewAppointDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


/**
 * 本控制台用于医生查看预约详情
 * 在预约界面可以选择查看今日预约和近期预约的患者名单
 * 在近期预约界面选择某个日期可以查看该日期内未就诊的患者名单
 * 点击该患者可以显示其预约详情
 */
@Controller
public class DoctorViewAppointDetailsController {

    @Autowired
    DoctorViewAppointDetailsService doctorViewAppointDetailsService;
    /**
     * 医生查看今日预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/getPatientTodayServlet")
    public @ResponseBody
    String doctorViewToday(@RequestParam("doctorID") String tel) {
        System.out.println("医生查看今日预约详情");
        try {
            DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
            int id=doctorInfoForm.getDoctorId();
            //通过医生ID查找该医生的预约信息
            ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOrderBydoctorId(id);
            if(orderForms.size()==0)
            {
                return "空";
            }
            else
            {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                String current_date=format.format(System.currentTimeMillis());
                int dateNum=0;
                ArrayList<OrderForm> orderFormsToday=new ArrayList<>();
                //数组存储预约日期是今天，并且进度为未就诊的患者id
                int []patientId=new int[orderForms.size()+1];
                //预约日期是今天，并且进度为未就诊的预约id
                int []bookId=new int[orderForms.size()+1];
                String []bookDate=new String[orderForms.size()+1];
                String []bookTime=new String[orderForms.size()+1];
                for(int i=0;i<orderForms.size();i++)
                {
                    OrderForm orderForm=orderForms.get(i);
                    String book_date=format.format(orderForm.getBootDate());
                    if(book_date.equals(current_date)&&orderForm.getProgress().equals("未就诊"))
                    {
                        orderFormsToday.add(orderForm);
                        patientId[dateNum]=orderForm.getPatientId();
                        bookId[dateNum]=orderForm.getBootId();
                        bookDate[dateNum]=format.format(orderForm.getBootDate());
                        bookTime[dateNum]=orderForm.getBootTime();
                        dateNum++;
                        System.out.println(orderForm.getBootId());
                    }
                }
                //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                ArrayList<DoctorViewPatient> doctorViewPatients=new ArrayList<>();
                if(dateNum>0)
                {
                    for(int i=0;i<dateNum;i++)
                    {
                        DoctorViewPatient doctorViewPatient=new DoctorViewPatient();
                        doctorViewPatient.setOrderDate(bookDate[i]);
                        doctorViewPatient.setOrderTime(bookTime[i]);
                        UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                        System.out.println(userLoginForm.getUserId());
                        doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                        doctorViewPatient.setPatientName(userLoginForm.getUserName());
                        doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                        doctorViewPatients.add(doctorViewPatient);
                        System.out.println(doctorViewPatient.toString());
                    }
                    //按预约日期排序
                    Comparator<DoctorViewPatient> comparator = new Comparator<DoctorViewPatient>(){
                        public int compare(DoctorViewPatient s1, DoctorViewPatient s2) {
                            //预约日期相同则按照预约时间排序
                            return s1.orderTime.compareTo(s2.orderTime);
                        }
                    };
                    //这里就会自动根据规则进行排序
                    Collections.sort(doctorViewPatients,comparator);
                    return doctorViewPatients.toString();
                }
                else
                {
                    return "空";
                }
            }
        } catch (Exception e) {
            return "False";
        }
    }

    /**
     * 医生查看近期预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/getPatientServlet")
    public @ResponseBody
    String doctorViewRecent(@RequestParam("chooseFlag") String chooseFlag,
                            @RequestParam("chooseDate") String chooseDate,
                            @RequestParam("doctorTel") String tel) {
        System.out.println("医生查看近期预约详情");
        try
        {
            if(chooseFlag.equals("true")) //chooseFlag为TRUE表示按搜索日期chooseDate显示患者名单
            {
                DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
                int id=doctorInfoForm.getDoctorId();
                //通过医生ID查找该医生的预约信息
                ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOrderBydoctorId(id);
                if(orderForms.size()==0)
                {
                    System.out.println("该医生没有预约信息");
                    return "空";
                }
                else
                {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                    int dateNum=0;
                    //数组存储预约日期为搜索日期，并且进度为未就诊的患者id
                    int []patientId=new int[orderForms.size()+1];
                    //预约日期为搜索日期，并且进度为未就诊的预约id
                    int []bookId=new int[orderForms.size()+1];
                    //预约日期
                    String []bookDate=new String[orderForms.size()+1];
                    String []bookTime=new String[orderForms.size()+1];
                    for(int i=0;i<orderForms.size();i++)
                    {
                        OrderForm orderForm=orderForms.get(i);
                        String book_date=format.format(orderForm.getBootDate());
                        if(book_date.equals(chooseDate)&&orderForm.getProgress().equals("未就诊"))
                        {
                            patientId[dateNum]=orderForm.getPatientId();
                            bookId[dateNum]=orderForm.getBootId();
                            bookDate[dateNum]=format.format(orderForm.getBootDate());
                            bookTime[dateNum]=orderForm.getBootTime();
                            dateNum++;
                            System.out.println(orderForm.getBootId());
                        }
                    }
                    //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                    ArrayList<DoctorViewPatient> doctorViewPatients=new ArrayList<>();
                    for(int i=0;i<dateNum;i++)
                    {
                        DoctorViewPatient doctorViewPatient=new DoctorViewPatient();
                        doctorViewPatient.setOrderDate(bookDate[i]);
                        doctorViewPatient.setOrderTime(bookTime[i]);
                        UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                        doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                        doctorViewPatient.setPatientName(userLoginForm.getUserName());
                        doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                        doctorViewPatients.add(doctorViewPatient);
                    }
                    if(dateNum>0)
                    {
                        //按预约日期排序
                        Comparator<DoctorViewPatient> comparator = new Comparator<DoctorViewPatient>(){
                            public int compare(DoctorViewPatient s1, DoctorViewPatient s2) {
                                //先按照预约日期排序
                                if(!s1.orderDate.equals(s2.orderDate)){
                                    return s1.orderDate.compareTo(s2.orderDate);
                                }
                                else{
                                    //预约日期相同则按照预约时间排序
                                    return s1.orderTime.compareTo(s2.orderTime);
                                }
                            }
                        };
                        //这里就会自动根据规则进行排序
                        Collections.sort(doctorViewPatients,comparator);
                        System.out.println(doctorViewPatients.toString());
                        return doctorViewPatients.toString();
                    }
                    else
                    {
                        return "空";
                    }
                }
            }
            else if(chooseFlag.equals("false"))
            {
                DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
                int id=doctorInfoForm.getDoctorId();
                //通过医生ID查找该医生的预约信息
                ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOrderBydoctorId(id);
                if(orderForms.size()==0)
                {
                    return "空";
                }
                else
                {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                    String current_date=format.format(System.currentTimeMillis());
                    int dateNum=0;
                    //数组存储预约日期大于今天，并且进度为未就诊的患者id
                    int []patientId=new int[orderForms.size()+1];
                    //预约日期大于今天，并且进度为未就诊的预约id
                    int []bookId=new int[orderForms.size()+1];
                    //预约日期
                    String []bookDate=new String[orderForms.size()+1];
                    String []bookTime=new String[orderForms.size()+1];
                    for(int i=0;i<orderForms.size();i++)
                    {
                        OrderForm orderForm=orderForms.get(i);
                        String book_date=format.format(orderForm.getBootDate());
                        if(book_date.compareTo(current_date)>0&&orderForm.getProgress().equals("未就诊"))
                        {
                            patientId[dateNum]=orderForm.getPatientId();
                            bookId[dateNum]=orderForm.getBootId();
                            bookDate[dateNum]=format.format(orderForm.getBootDate());
                            bookTime[dateNum]=orderForm.getBootTime();
                            dateNum++;
                            System.out.println(orderForm.getBootId());
                        }
                    }
                    //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                    ArrayList<DoctorViewPatient> doctorViewPatients=new ArrayList<>();
                    for(int i=0;i<dateNum;i++)
                    {
                        DoctorViewPatient doctorViewPatient=new DoctorViewPatient();
                        doctorViewPatient.setOrderDate(bookDate[i]);
                        doctorViewPatient.setOrderTime(bookTime[i]);
                        UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                        doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                        doctorViewPatient.setPatientName(userLoginForm.getUserName());
                        doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                        doctorViewPatients.add(doctorViewPatient);
                    }
                    System.out.println(dateNum);
                    if(dateNum>0)
                    {
                        //按预约日期排序
                        Comparator<DoctorViewPatient> comparator = new Comparator<DoctorViewPatient>(){
                            public int compare(DoctorViewPatient s1, DoctorViewPatient s2) {
                                //先按照预约日期排序
                                if(!s1.orderDate.equals(s2.orderDate)){
                                    return s1.orderDate.compareTo(s2.orderDate);
                                }
                                else{
                                    //预约日期相同则按照预约时间排序
                                    return s1.orderTime.compareTo(s2.orderTime);
                                }
                            }
                        };
                        //这里就会自动根据规则进行排序
                        Collections.sort(doctorViewPatients,comparator);
                        System.out.println(doctorViewPatients.toString());
                        return doctorViewPatients.toString();
                    }
                    else
                    {
                        return "空";
                    }
                }
            }
            else
            {
                return "False";
            }
        } catch (Exception e) {
            return "False";
        }
    }
}